from utils.models import BaseModel
import user
import car
import store
from django.db import models


class Order(BaseModel):
    '''订单模型'''
    status_choices = (
        (0, '未支付'),
        (1, '已支付'),
        (2, '已取消'),
        (3, '超时取消'),
    )

    pay_choices = (
        (1, '支付宝'),
        (2, '微信支付'),
    )
    subject = models.CharField(max_length=150, verbose_name="订单标题")
    total_amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="订单总价", default=0)
    out_trade_no = models.CharField(max_length=64, verbose_name="订单号", unique=True)
    trade_no = models.CharField(max_length=64, null=True, verbose_name="流水号")
    order_status = models.SmallIntegerField(choices=status_choices, default=0, verbose_name="订单状态")
    pay_type = models.SmallIntegerField(choices=pay_choices, default=1, verbose_name="支付方式")
    pay_time = models.DateTimeField(auto_now_add=True, null=True, verbose_name="支付时间")
    user = models.ForeignKey('user.User', related_name='user_orders', on_delete=models.DO_NOTHING, db_constraint=False,
                             verbose_name="下单用户")
    store = models.ForeignKey('store.Store', related_name='store_orders', on_delete=models.DO_NOTHING,
                              db_constraint=False,
                              verbose_name="门店")

    class Meta:
        db_table = "car_rent_order"
        verbose_name_plural = "订单记录"

    def __str__(self):
        return "%s - ￥%s" % (self.subject, self.total_amount)

    # @property
    # def courses(self):
    #     return pas


class OrderDetail(BaseModel):
    '''订单详情'''
    user = models.ForeignKey(
        'user.User',
        related_name='user_orderdetail',
        on_delete=models.CASCADE,
        db_constraint=False,
        verbose_name='用户'
    )
    order = models.ForeignKey(
        Order,
        related_name='order_orderdetail',
        on_delete=models.CASCADE,
        db_constraint=False,
        verbose_name="订单")
    car = models.ForeignKey(
        'car.Car',
        related_name='car_orderdetail',
        on_delete=models.DO_NOTHING,
        db_constraint=False,
        verbose_name="汽车"
    )

    tenancy = models.IntegerField(
        null=True,
        verbose_name='租期'
    )

    price = models.IntegerField(
        verbose_name='日均价格'
    )

    class Meta:
        db_table = "car_order_detail"
        verbose_name_plural = "订单详情"

    def __str__(self):
        return "%s的订单：%s" % (self.order.user.username, self.order.out_trade_no)
